//
//  UITextField+Extension.swift
//  ZYA
//
//  Created by WB Zhou on 2017/4/11.
//  Copyright © 2017年 aowang. All rights reserved.
//

import Foundation
import UIKit
extension UITextField {
    
    // label加下划线
     func underline() {
          if let textString = self.text {
              let attributedString = NSMutableAttributedString(string: textString)
              attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: NSRange(location: 0, length: attributedString.length))
              attributedText = attributedString
          }
      }
    
    
    
    func isEmpty() -> Bool {
        return self.text?.count == 0
    }
    
    /// 保留两位小数
    func validateTwoDecimal() -> Bool {
        
        //        let regex = "^[0-9]+(.[0-9]{2})?$"
        let regex = "^[0-9]*((\\.|,)[0-9]{0,2})?$"
        return self.validateWithRegex(regex: regex)
    }
    
    /// 校验身份证
    func validateIDCard() -> Bool {
        
        let regex15 = "^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$"
        let regex18 = "^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9xX])$"
        
        //        let regex = "^\\d{15}(\\d\\d[0-9xX])?"
        //        return self.validateWithRegex(regex: regex)
        return self.validateWithRegex(regex: regex15) || self.validateWithRegex(regex: regex18)
    }
    
    /// 校验手机号
    func validatePhonenumber() -> Bool {
        let regex = "^(1)\\d{10}$"
        return self.validateWithRegex(regex: regex)
    }
    
    /// 密码的强度必须是包含大小写字母和数字的组合，不能使用特殊字符，长度在6-18之间
    func validatePassword() -> Bool {
        let regex = "^(?=.*\\d)(?=.*[a-zA-Z]).{6,18}$"
        return self.validateWithRegex(regex: regex)
    }
    // 正则表达式校验
    func validateWithRegex(regex: String) -> Bool {
        let predicate = NSPredicate(format: "SELF MATCHES %@", regex)
        return predicate.evaluate(with: self.text)
    }
}
typealias TextFieldEditAction = (UITextField)->()
extension UITextField {
    private struct AssociatetapdKeys{
        static var actionKey = "actiontapKey"
        static var actionEndEditKey = "actionEndEditKey"
    }
    @objc dynamic var action: TextFieldEditAction? {
        set{
            objc_setAssociatedObject(self,&AssociatetapdKeys.actionKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        get{
            if let action = objc_getAssociatedObject(self, &AssociatetapdKeys.actionKey) as? TextFieldEditAction {
                return action
            }
            return nil
        }
    }
    @objc dynamic var actionEndEdit: TextFieldEditAction? {
        set{
            objc_setAssociatedObject(self,&AssociatetapdKeys.actionEndEditKey, newValue, objc_AssociationPolicy.OBJC_ASSOCIATION_COPY)
        }
        get{
            if let action = objc_getAssociatedObject(self, &AssociatetapdKeys.actionEndEditKey) as? TextFieldEditAction {
                return action
            }
            return nil
        }
    }
    func addTextEditEndClosure(_ action:@escaping  TextFieldEditAction) {
        self.actionEndEdit = action
         self.addTarget(self, action: #selector(textEndEditAction(textfield:)), for: .editingDidEnd)
    }
    func addTextChangeClosure(_ action:@escaping  TextFieldEditAction) {
        self.action = action
         self.addTarget(self, action: #selector(textChangeAction(textfield:)), for: .editingChanged)
    }
    @objc func textEndEditAction(textfield: UITextField) {
         if let action = self.actionEndEdit {
             action(textfield)
         }
    }
    @objc func textChangeAction(textfield: UITextField) {
         if let action = self.action {
             action(textfield)
         }
    }
}

 
